package handler

import (
	"admin-ease/global"
	"admin-ease/internal/dto"
	"admin-ease/internal/model"
	"admin-ease/pkg"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type MessHandel struct {
}

func NewMessHandel() *MessHandel {
	return &MessHandel{}
}
func (m MessHandel) MessList(c *gin.Context) {
	var userId dto.Mess
	err := c.ShouldBind(&userId)
	if err != nil {
		c.JSON(http.StatusBadRequest, dto.UserResp{
			Msg:  "参数获取失败",
			Code: 500,
		})
		return
	}
	var mess []model.Mess
	err = global.DB.Debug().Where("user_id=? and type=?", userId.UserId, userId.Type).Find(&mess).Error
	if err != nil {
		c.JSON(http.StatusBadRequest, dto.UserResp{
			Msg:  "信息公告获取失败",
			Code: 500,
		})
		return
	}
	simple := pkg.NewRabbitMQSimple("Comment_message")
	msgs := simple.ConsumeSimple()
	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("未读消息: %s", d.Body)

		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
	c.JSON(http.StatusOK, dto.UserResp{
		Msg:  "信息公告获取成功",
		Code: 200,
		Data: mess,
	})
	return
}
